<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator="/" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">告警设置</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home" @click="crumbsClick">告警分组</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item v-if="typeDialog">
          <span class="main-breadcrumb-sp">{{ crumbsStatus }}</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content" v-if="!typeDialog">
      <div class="flex-bet">
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
          <!-- <el-form-item label="" prop="companyId">
                        <el-select v-if="userType == 0 || userType == 1" v-model="searchForm.companyId"
                            placeholder="请选择机构名称" @change="companyChange" clearable>
                            <el-option v-for="item in companyArray" :key="item.id" :label="item.name"
                                :value="item.id"></el-option>
                        </el-select>
                    </el-form-item> -->
          <el-form-item label="">
            <el-cascader v-if="userType == 0" v-model="searchForm.companyId" placeholder="请选择公司名称" :options="companyArray" :props="{
                checkStrictly: true,
                value: 'id',
                label: 'name',
                children: 'childList',
              }" style="width: 150px" @change="changeTreeItem" clearable></el-cascader>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="searchForm.baseId" placeholder="请选择基地名称" @change="onSubmit" style="width: 150px !important" clearable>
              <el-option v-for="item in baseArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="modelAlarmSettingsId">
            <el-select v-model="searchForm.modelAlarmSettingsId" placeholder="请选择告警模板" @change="onSubmit" clearable>
              <el-option v-for="item in modelAlarmSettingsArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button v-allow="'030202'" type="primary" @click="onSubmit">查询</el-button>
            <el-button v-allow="'030202'" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
        <div class="left-title flex">
          <el-button v-allow="'030201'" type="primary" @click="handleAdd()">添加告警分组</el-button>
        </div>

      </div>
      <NxTable :table-cols="tableCols" :table-data="tableData" :pag-ination="pagination" class="table-wrap" @refresh="refresh">
        <template slot="operation" slot-scope="scope">
          <span v-allow="'030203'" @click="handleEdit(scope.row)" class="nx-btn nx-edit">编辑</span>
          <span v-allow="'030204'" @click="handleDelete(scope.row)" class="nx-btn nx-del">删除</span>
        </template>
      </NxTable>
    </div>
    <AddEditDialog :addEditId="addEditId" :addEditStatus="addEditStatus" :addEditIsDialog="addEditIsDialog">
    </AddEditDialog>
  </div>
</template>
<script>
import NxTable from '@/components/NxTable.vue'
import AddEditDialog from './components/addEditDialog.vue'
import { Page } from '@/utils/pagination'
import { getModelAlarmGroupList, delModelAlarmGroup, getModelAlarmSettingss } from '@/api/alarm'
//   import { getCompanyInfos } from "@/api/company";
// import { getCompanyInfoTree } from '@/api/company'
// import { getSysCompanyBases } from '@/api/system'
import mixin from '@/api/CompanyInfoTree.js'
export default {
  mixins: [mixin],
  components: {
    NxTable,
    AddEditDialog
  },
  // 表格数据
  data() {
    return {
      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        companyId: '',
        baseId: '',
        modelAlarmSettingsId: ''
      },
      // 表格字段
      tableCols: [
        {
          label: '公司名称',
          prop: 'companyName',
          align: 'center'
        },
        {
          label: '告警模板',
          prop: 'modelAlarmSettingsName',
          align: 'center'
        },
        {
          label: '设备分组',
          prop: 'deviceGroupName',
          align: 'center'
        },
        {
          label: '操作',
          slot: 'operation',
          align: 'center',
          width: 180
        }
      ],
      tableData: [],
      pagination: new Page(),
      crumbsStatus: '', // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditStatus: 'add',
      addEditId: '', // 添加编辑rowid
      baseArray: [], // 基地集合
      companyArray: [], // 公司集合
      modelAlarmSettingsArray: [], // 告警模板数组
      userType: '', // 账号类型（0超级管理员，2组织管理员，3一般管理员）
      companyId: '' // 机构id
    }
  },
  // 初始化加载
  mounted() {
    this.userType = Number(window.localStorage.getItem('userType'))
    this.searchForm.userType = this.userType
    // 查询
    // 获取机构列表
    // this.getCompanyList();
    this.getCompanyInfoTree()
    this.getList()
    setTimeout(() => {
      this.getModelAlarmSettingsList()
    }, 1000)
  },
  // 自定义方法
  methods: {
    // 查询
    onSubmit() {
      this.searchForm.pageNum = 1
      this.pagination.pageNum = 1
      this.getList()
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum
      this.searchForm.pageSize = val.pageSize
      this.getList()
    },
    // 重置
    resetForm() {
      this.$refs['searchForm'].resetFields()

      if (this.userType == 2) {
        this.searchForm.companyId = this.companyId
      } else {
        // 清空
        this.modelAlarmSettingsArray = []
      }

      this.onSubmit()
    },
    // 查询列表
    getList() {
      getModelAlarmGroupList(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data
          this.pagination.count = res.count
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delModelAlarmGroup({ id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })
              this.getList()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 添加
    handleAdd() {
      this.addEditId = ''
      this.addEditStatus = 'add'
      this.addEditIsDialog = true
    },
    // 编辑
    handleEdit(row) {
      this.addEditId = row.id + ''
      this.addEditStatus = 'edit'
      this.addEditIsDialog = true
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false
      this.typeDialog = false
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false
      this.addEditIsDialog = false
    },
    // 获取机构列表
    getCompanyList() {
      getCompanyInfos().then((res) => {
        if (res.code == 200) {
          this.companyArray = res.data
        }
      })
    },
    // 机构下拉切换
    companyChange() {
      // 清空
      this.searchForm.modelAlarmSettingsId = ''
      this.modelAlarmSettingsArray = []

      // 获取告警模板列表
      this.getModelAlarmSettingsList()

      // 查询
      this.onSubmit()
    },
    // 获取公司列表
    // getCompanyInfoTree() {
    //   if (this.searchForm.userType == 0) {
    //     // 超级管理员，公司只有一个总公司
    //     getCompanyInfoTree().then((res) => {
    //       if (res.code == 200) {
    //         this.companyArray = res.data
    //       }
    //     })
    //   } else {
    //     getCompanyInfoTree({ id: 0 }).then((res) => {
    //       if (res.code == 200) {
    //         this.companyArray = res.data
    //       }
    //     })
    //   }
    // },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.searchForm.companyId = val[val.length - 1]
      } else {
        this.searchForm.companyId = ''
      }

      this.searchForm.baseId = ''
      this.baseArray = []

      this.getList()

      // 获取角色列表
      this.getSysCompanyBases()
      // 获取告警模板列表
      this.getModelAlarmSettingsList()
    },
    // 获取角色列表
    // getSysCompanyBases() {
    //   if (
    //     this.searchForm.companyId === '' ||
    //     this.searchForm.companyId == null
    //   ) {
    //     return
    //   }

    //   getSysCompanyBases({ id: this.searchForm.companyId }).then((res) => {
    //     if (res.code == 200) {
    //       this.baseArray = res.data
    //     }
    //   })
    // },
    // 获取告警模板列表
    getModelAlarmSettingsList() {
      if (!this.searchForm.companyId || this.searchForm.companyId == '') {
        return
      }
      getModelAlarmSettingss({ id: this.searchForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.modelAlarmSettingsArray = res.data
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;

  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;

    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }

    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>
